﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrameWork.Components;
using System.Collections;
using Slja.Model.Platform;

namespace FrameWork.web.Web.Project.SelPage
{
    public partial class UserSelect : System.Web.UI.Page
    {
        public Boolean OnlyOne
        {
            get { return Request.QueryString["onlyone"].ToUpper() == "TRUE"; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           // drpSelType.SelectedIndexChanged += new EventHandler(drpSelType_SelectedIndexChanged);
            btnQuery.Click += new EventHandler(btnQuery_Click);
            if (!IsPostBack)
            {
                BindGroup();
                BindRole();
                QueryUser();
            }
        }

        void btnQuery_Click(object sender, EventArgs e)
        {
            QueryUser();
        }
        void QueryUser()
        {
            string strWhere = " 1=1";
            if (drpSelType.SelectedValue == "NameOrEng")
            {
                string keyword = tbxKeyword.Text.Trim();
                keyword = Common.querySQL(keyword);
                strWhere += " and (u.U_CName like '" + keyword + "%' or u.U_EName like '" + keyword + "%')";
            }
            else if (drpSelType.SelectedValue == "Role")
            {
                strWhere += " and ur.R_RoleID=" + drpRole.SelectedValue;
            }
            else if (drpSelType.SelectedValue == "Group")
            {
                strWhere += " and u.U_GroupID=" + drpGroup.SelectedValue;
            }

            Slja.BLL.Platform.UserBLL bll = new Slja.BLL.Platform.UserBLL();
            int pageSize = Pager1.PageSize;
            int curPage = Pager1.CurrentPageIndex;
            int recordCount;

            List<UserModel> users = bll.GetModelList(strWhere, pageSize, curPage, out recordCount);
            Pager1.RecordCount = recordCount;
            Repeater1.DataSource = users;
            Repeater1.DataBind();
        }
        protected void Pager1_PageChanged(object sender, EventArgs e)
        {
            QueryUser();
        }
        void BindGroup()
        {            
            int TotalRecord = 0;
            QueryParam qp = new QueryParam();
            qp.Where = " where 1=1 " ;//+ GroupID;
            qp.PageIndex = 1;
            qp.PageSize = 100;
            ArrayList lst = BusinessFacade.sys_GroupList(qp, out TotalRecord);
            foreach (sys_GroupTable x in lst)
            {
                ListItem li = new ListItem(x.G_CName, x.GroupID.ToString());
                drpGroup.Items.Add(li);
            }
        }
        void BindRole()
        {
            int TotalRecord = 0;
            QueryParam qp = new QueryParam();
            qp.Where = " where 1=1 ";//+ GroupID;
            qp.PageIndex = 1;
            qp.PageSize = 100;
            ArrayList lst = BusinessFacade.sys_RolesList(qp, out TotalRecord);
            foreach (sys_RolesTable x in lst)
            {
                ListItem li = new ListItem(x.R_RoleName, x.RoleID.ToString());
                drpRole.Items.Add(li);
            }
        }
        protected void drpSelType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drpSelType.SelectedValue == "NameOrEng")
            {
                tbxKeyword.Visible = true;
                drpRole.Visible = false;
                drpGroup.Visible = false;
            }
            else if(drpSelType.SelectedValue == "Role")
            {
                tbxKeyword.Visible = false;
                drpRole.Visible =true ;
                drpGroup.Visible = false;
            }
            else if (drpSelType.SelectedValue == "Group")
            {
                tbxKeyword.Visible = false;
                drpRole.Visible = false;
                drpGroup.Visible = true;
            }
        }
    }
}